local PlayerPlane = {}
local t_concat = table.concat
local t_rm 	= table.remove
local t_sort = table.sort
local math_ceil = math.ceil

local p_plane_mt = {__index = PlayerPlane}

local player_plane_size = {
	width = 186,
	height = 130,
}
 
 local enemy_plane_size = {
	width = 120,
	height = 79,
 }
 
 local bullet_size = {
	width = 62,
	height = 108,
 }

function PlayerPlane:ctor(idx)
	self._idx = idx;
	self._posX = 0;
	self._posY = 0;
	self._speed = 0;
	self._width = 0;
	self._height = 0;
	self._firetime = 0;
	self._blood = 100;
	self._attack = 0;
	self._defensive = 0;
	self._survival = true;				-- 是否存活标记
	self._bfit = false;					-- 是否合体中
	
	self._bStartFitState = false;		-- 是否开始合体
	self._bFitTime = 0;					-- 合体已经进行的时间
end

function PlayerPlane:getInfo()
	return {
		idx = self._idx,
		posX = self._posX,
		posY = self._posY,
		speed = self._speed,
		width = self._width,
		height = self._height,
		blood = self._blood,
		attack = self._attack,
		defensive = self._defensive,
		bfit = self._bfit,
	}
end

function PlayerPlane:setPositon(pos)
	self._posX = pos.nXpos or 0;
	self._posY = pos.nYpos or 0;
end

function PlayerPlane:getPosition()
	return 
	{
		nXpos = self._posX, 
		nYpos = self._posY,
	};
end

function PlayerPlane:setFitState(bfit)
	self._bfit = bfit;
end

function PlayerPlane:getFitState()
	return self._bfit;
end

-- 开始合体时间
function PlayerPlane:getStartFitState()
	return self._bStartFitState;
end

function PlayerPlane:setStartFitState(fitstate)
	self._bStartFitState = fitstate
end 

-- 合体已经进行的时间
function PlayerPlane:getFitTime()
	return self._bFitTime;
end

function PlayerPlane:setFitTime(fitTime)
	self._bFitTime = fitTime;
end

-- 合体增加时间
function PlayerPlane:addFitTime()
	self._bFitTime = self._bFitTime + 1;
	if self._bFitTime > 100 then
		self._bFitTime = 0;
		return true;
	end
	return false;
end

function PlayerPlane:isFire()
	if not self._survival then
		return false;
	end
	self._firetime = self._firetime - 1;
	if self._firetime <= 0 then
		self._firetime = 30;
		return true;
	end
	return false;
end

--受伤
function PlayerPlane:injured(object)
	-- 计算受伤值
	local jureBlood = object.attack - self._defensive
	self._blood = self._blood - jureBlood;
	local data = {
		bExist = false,
		disblood = 0,
	}
	data.disblood = -jureBlood;
	if self._blood <= 0 then
		data.bExist = false
	else 
		data.bExist = true
	end
	return data
end

-- 分摊的伤害
function PlayerPlane:injuredByFitPlane(nblood)
	self._blood = self._blood - nblood;
end

--玩家出局
function PlayerPlane:playerOut()
	print("PlayerPlane ".. self._idx .. " is out")
	self._survival = false;
end

function PlayerPlane:isOut()
	return self._survival == false and true or false;
end

function PlayerPlane.new(...)
	local instance = {}
	setmetatable(instance, p_plane_mt)
	instance:ctor(...)
	return instance
end

return PlayerPlane
